﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class KBCLSBO : Csla.BusinessBase<KBCLSBO>
    {
        #region Business Properties

        private int? _KBCLSID = null;
        public int? KBCLSID
        {
            get { return this._KBCLSID; }
            set { this._KBCLSID = value; }
        }

        private int? _LoaiCLSID = null;
        public int? LoaiCLSID
        {
            get { return this._LoaiCLSID; }
            set { this._LoaiCLSID = value; }
        }

        private int? _Status = 0;
        public int? Status
        {
            get { return this._Status; }
            set { this._Status = value; }
        }

        private int? _BenhNhanID = null;
        public int? BenhNhanID
        {
            get { return this._BenhNhanID; }
            set { this._BenhNhanID = value; }
        }

        private int? _NguoiThucHienID = null;
        public int? NguoiThucHienID
        {
            get { return this._NguoiThucHienID; }
            set { this._NguoiThucHienID = value; }
        }

        private DateTime? _NgayThucHien = null;
        public DateTime? NgayThucHien
        {
            get { return this._NgayThucHien; }
            set { this._NgayThucHien = value; }
        }

        private string _Ten = null;
        public string Ten
        {
            get { return this._Ten; }
            set { this._Ten = value; }
        }

        private string _KetQua = null;
        public string KetQua
        {
            get { return this._KetQua; }
            set { this._KetQua = value; }
        }

        private string _KetLuan = null;
        public string KetLuan
        {
            get { return this._KetLuan; }
            set { this._KetLuan = value; }
        }

        private string _GhiChu = null;
        public string GhiChu
        {
            get { return this._GhiChu; }
            set { this._GhiChu = value; }
        }

        private bool? _Deleted = false;
        public bool? Deleted
        {
            get { return this._Deleted; }
            set { this._Deleted = value; }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _KBCLSID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newKBCLSID = -1;
        private KBCLSBO()
        {
            _KBCLSID = newKBCLSID--;
        }

        public KBCLSBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public KBCLSBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static KBCLSBO NewRootKBCLSBO()
        {
            return DataPortal.Create<KBCLSBO>(new RootCriteria());
        }

        internal static KBCLSBO NewChildKBCLSBO()
        {
            return DataPortal.Create<KBCLSBO>(new ChildCriteria());
        }

        public static KBCLSBO GetRootKBCLSBO(int _KBCLSID)
        {
            return DataPortal.Fetch<KBCLSBO>(new RootCriteria(_KBCLSID));
        }

        public static KBCLSBO GetRootKBCLSBOByWhere(string where)
        {
            return DataPortal.Fetch<KBCLSBO>(where);
        }

        internal static KBCLSBO GetChildKBCLSBO(SqlDataReader dataReader)
        {
            return new KBCLSBO(dataReader);
        }

        public static void DeleteKBCLSBO(int _KBCLSID)
        {
            DataPortal.Delete(new RootCriteria(_KBCLSID));
        }

        public static void DeleteKBCLSBO(int _KBCLSID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_KBCLSID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int KBCLSID;
            public bool directDelete = false;
            public RootCriteria(int _KBCLSID)
            {
                this.KBCLSID = _KBCLSID;
                this.directDelete = false;
            }

            public RootCriteria(int _KBCLSID, bool directDelete)
            {
                this.KBCLSID = _KBCLSID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int KBCLSID;
            public bool directDelete = false;
            public ChildCriteria(int _KBCLSID)
            {
                this.KBCLSID = _KBCLSID;
                this.directDelete = false;
            }

            public ChildCriteria(int _KBCLSID, bool directDelete)
            {
                this.KBCLSID = _KBCLSID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
        }

        private void This_Fetch(string where)
        {
        }

        private void Fetch(SqlDataReader dataReader)
        {
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_KBCLSID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_KBCLSID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
